package cn.tedu.carehome.foundation.service.impl;

import cn.tedu.carehome.foundation.ex.ServiceException;
import cn.tedu.carehome.foundation.mapper.CaseFileMapper;
import cn.tedu.carehome.foundation.mapper.IllMapper;
import cn.tedu.carehome.foundation.mapper.MedicineMapper;
import cn.tedu.carehome.foundation.mapper.OlderMapper;
import cn.tedu.carehome.foundation.pojo.dto.IllAddNewDTO;
import cn.tedu.carehome.foundation.pojo.dto.IllUpdateDTO;
import cn.tedu.carehome.foundation.pojo.entity.CaseFile;
import cn.tedu.carehome.foundation.pojo.entity.Ill;
import cn.tedu.carehome.foundation.pojo.vo.IllListItemVO;
import cn.tedu.carehome.foundation.pojo.vo.IllStandardVO;
import cn.tedu.carehome.foundation.pojo.vo.MedicineStandardVO;
import cn.tedu.carehome.foundation.pojo.vo.OlderStandardVO;
import cn.tedu.carehome.foundation.restful.JsonPage;
import cn.tedu.carehome.foundation.service.IIllService;
import cn.tedu.carehome.foundation.web.ServiceCode;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
@Slf4j
public class IllServiceImpl implements IIllService {

    @Autowired
    private IllMapper illMapper;

    @Autowired
    private OlderMapper olderMapper;

    @Autowired
    private MedicineMapper medicineMapper;

    @Autowired
    private CaseFileMapper caseFileMapper;

    @Override
    public void addNew(IllAddNewDTO illAddNewDTO) {
        log.debug("开始处理【添加病例】的业务，参数：{}", illAddNewDTO);

        OlderStandardVO older = olderMapper.getStandardById(illAddNewDTO.getOlderId());
        if (older == null) {
            String message = "添加病例失败，选择的老人不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }

        MedicineStandardVO medicine = medicineMapper.getStandardById(illAddNewDTO.getMedicineId());
        if (medicine == null) {
            String message = "添加病例失败，选择的药品不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }


        Ill ill = new Ill();
        BeanUtils.copyProperties(illAddNewDTO, ill);
        int rows = illMapper.insert(ill);
        if (rows != 1) {
            String message = "添加病例失败，服务器忙，请稍后重试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }

        CaseFile caseFile = new CaseFile();
        caseFile.setIllId(ill.getId());
        caseFile.setOlderId(illAddNewDTO.getOlderId());
        caseFile.setMedicineId(illAddNewDTO.getMedicineId());
        caseFile.setMedicineTime(illAddNewDTO.getMedicineTime());
        caseFile.setInfo(illAddNewDTO.getInfo());
        rows = caseFileMapper.insert(caseFile);
        if (rows != 1) {
            String message = "添加病例失败，服务器忙，请稍后重试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }
    }

    @Override
    public void deleteById(Long id) {
        log.debug("开始处理【根据id删除病例】的业务，参数：{}", id);

        IllStandardVO ill = illMapper.getStandardById(id);
        if (ill == null) {
            String message = "删除病例失败，尝试访问的病例不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }


        int rows = illMapper.deleteById(id);
        if (rows != 1) {
            String message = "删除病例失败，服务器忙，请稍后重试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_DELETE, message);
        }

        rows = caseFileMapper.deleteById(id);
        if (rows < 1) {
            String message = "删除病例失败，服务器忙，请稍后重试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_DELETE, message);
        }

    }

    @Override
    public void updateInfoById(IllUpdateDTO illUpdateDTO) {
        log.debug("开始处理【根据id修改病例详情】的业务，参数：{}", illUpdateDTO);

        IllStandardVO queryResult = illMapper.getStandardById(illUpdateDTO.getId());
        if (queryResult == null) {
            String message = "修改病例详情失败，尝试访问的病例不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }

        Ill ill = new Ill();
        BeanUtils.copyProperties(illUpdateDTO, ill);
        int rows = illMapper.update(ill);
        if (rows != 1) {
            String message = "修改病例详情失败，服务器忙，请稍后重试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_UPDATE, message);
        }

        CaseFile caseFile = new CaseFile();
        caseFile.setIllId(illUpdateDTO.getId());
        caseFile.setOlderId(illUpdateDTO.getOlderId());
        caseFile.setMedicineId(illUpdateDTO.getMedicineId());
        caseFile.setMedicineTime(illUpdateDTO.getMedicineTime());
        caseFile.setInfo(illUpdateDTO.getInfo());
        rows = caseFileMapper.update(caseFile);
        if (rows != 1) {
            String message = "修改病例失败，服务器忙，请稍后重试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_UPDATE, message);
        }

    }

    @Override
    public IllStandardVO getStandardById(Long id) {
        log.debug("开始处理【根据id查询病例详情】的业务，参数：{}", id);

        IllStandardVO ill = illMapper.getStandardById(id);
        if (ill == null) {
            String message = "查询病例失败，尝试访问的病例不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        return ill;
    }

    @Override
    public List<IllListItemVO> list() {
        log.debug("开始处理【查询病例列表】的业务，无参数");
        List<IllListItemVO> list = illMapper.list();
        return list;
    }

    @Override
    public JsonPage<IllListItemVO> listIllsByPage(Integer page, Integer pageSize) {
        log.debug("处理【分页查询病例信息】的业务，页码：{}，每页条数：{}", page, pageSize);
        PageHelper.startPage(page, pageSize);
        List<IllListItemVO> list = illMapper.list();
        return JsonPage.restPage(new PageInfo<>(list));
    }


}
